package it.poste.il.compensatore.dao.impl;

import java.util.Properties;

import it.poste.il.compensatore.dao.MessageStatusDao;
import it.poste.il.compensatore.entities.MessageStatus;
import it.poste.il.compensatore.utilities.LoadQueriesSQL;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of MessageStatusDao.
 */
public class MessageStatusDaoImpl extends BaseEntityDaoImpl<MessageStatus> implements MessageStatusDao {
	public MessageStatusDaoImpl() {
        super(MessageStatus.class, "MessageStatus");
    }

    LoadQueriesSQL loadQueriesSQL;
    
	public LoadQueriesSQL getLoadQueriesSQL() {
		return loadQueriesSQL;
	}

	public void setLoadQueriesSQL(LoadQueriesSQL loadQueriesSQL) {
		this.loadQueriesSQL = loadQueriesSQL;
	}

	/**
	 * Take the message status by id.
	 * @param id Id of Message Status.
	 * @return MessageStatus Message Status from id.
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public MessageStatus findById(String id) {
		Properties p = loadQueriesSQL.getProperties();
		String querySQL = p.getProperty("findMessageStatusById");
		Query query = getEntityManager().createNativeQuery(querySQL, MessageStatus.class).
				setParameter("id", id);
		return (MessageStatus) query.getResultList().get(0);
	}

	/**
	 * Take the message status's description.
	 * @param msgSts Entity Message Status.
	 * @return String Description of the message status.
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String selectDescription(MessageStatus msgSts) {
		try {
			Properties p = loadQueriesSQL.getProperties();
			String querySQL = p.getProperty("selectMessageStatusDescription");
			Query query = getEntityManager().createNativeQuery(querySQL, MessageStatus.class).
					setParameter("id", msgSts.getId());
			return (String) query.getResultList().get(0);
		} catch (NoResultException e) {
		      return null;
		}
	}
}
